<?php
namespace app\controller;

use app\BaseController;
use app\service\EthnicityService;

class EthnicityController extends BaseController
{
    protected $ethnicityService;

    public function __construct(EthnicityService $ethnicityService)
    {
        $this->ethnicityService = $ethnicityService;
    }

    /**
     * 获取人种列表
     * @return \think\Response
     */
    public function list()
    {
        try {
            // 获取请求参数
            $scene = input('scene', '');
            $pageNo = input('pageNo/d', 1);
            $pageSize = input('pageSize/d', 10);

            // 验证必传参数
            if (empty($scene)) {
                return json([
                    'code' => 400,
                    'message' => 'scene参数为必传项',
                    'result' => null
                ]);
            }

            // 验证scene参数值
            if (!$this->ethnicityService->validateScene($scene)) {
                $supportedScenes = $this->ethnicityService->getSupportedScenes();
                return json([
                    'code' => 400,
                    'message' => 'scene参数值只能是: ' . implode(', ', array_keys($supportedScenes)),
                    'result' => null
                ]);
            }

            // 调用业务服务
            $data = $this->ethnicityService->getEthnicityList($scene, $pageNo, $pageSize);
            
            return json([
                'code' => 200,
                'message' => 'success',
                'result' => $data
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'message' => $e->getMessage(),
                'result' => null
            ]);
        }
    }

    /**
     * 根据ID获取人种信息
     * @return \think\Response
     */
    public function detail()
    {
        try {
            $ethnicityId = input('ethnicityId', '');
            $scene = input('scene', '');

            if (empty($ethnicityId)) {
                return json([
                    'code' => 400,
                    'message' => 'ethnicityId参数为必传项',
                    'result' => null
                ]);
            }

            if (empty($scene)) {
                return json([
                    'code' => 400,
                    'message' => 'scene参数为必传项',
                    'result' => null
                ]);
            }

            if (!$this->ethnicityService->validateScene($scene)) {
                $supportedScenes = $this->ethnicityService->getSupportedScenes();
                return json([
                    'code' => 400,
                    'message' => 'scene参数值只能是: ' . implode(', ', array_keys($supportedScenes)),
                    'result' => null
                ]);
            }

            $data = $this->ethnicityService->getEthnicityById($ethnicityId, $scene);
            
            if ($data === null) {
                return json([
                    'code' => 404,
                    'message' => '未找到对应的人种信息',
                    'result' => null
                ]);
            }

            return json([
                'code' => 200,
                'message' => 'success',
                'result' => $data
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'message' => $e->getMessage(),
                'result' => null
            ]);
        }
    }

    /**
     * 获取支持的场景列表
     * @return \think\Response
     */
    public function scenes()
    {
        try {
            $data = $this->ethnicityService->getSupportedScenes();
            
            return json([
                'code' => 200,
                'message' => 'success',
                'result' => $data
            ]);
        } catch (\Exception $e) {
            return json([
                'code' => 500,
                'message' => $e->getMessage(),
                'result' => null
            ]);
        }
    }
} 